//
//  LYChatVoiceCell.swift
//  WeChat
//
//  Created by mac on 2018/8/27.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit
import SnapKit

private let kChatVoiceBubbleTopTransparentGapValue: CGFloat = 7//气泡顶端有大约7像素的透明部分，绿色部分需要和头像持平
private let kChatVoicePlayingMarginLeft: CGFloat = 16 //播放小图标距离气泡箭头的值
private let kChatVoiceMaxWidth: CGFloat = 200

class LYChatVoiceCell: LYChatBaseCell {

    var listenVoiceButton: UIButton! {
        didSet{
            listenVoiceButton.imageView!.animationDuration = 1
            listenVoiceButton.isSelected = false
        }
    }
    var durationLabel: UILabel!{
        didSet{
            durationLabel.textColor = UIColor.white
            durationLabel.font = UIFont.systemFont(ofSize: 12)
        }
    }
    
    override func setNeedsDisplay() {
        super.setNeedsDisplay()
        
        listenVoiceButton = UIButton()
        
        durationLabel = UILabel(frame: CGRect(x: 259, y: 34, width: 24, height: 38))
        
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(listenVoiceButton)
        self.contentView.addSubview(durationLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setCellContent(_ model: ChatModel) {
        super.setCellContent(model)
        self.durationLabel.text = String(format: "%zd\"", Int(model.audioModel!.duration!))
        
        //设置 normal 背景Image
        let stretchImage = model.fromMe ? LYAsset.SenderTextNodeBkg.image : LYAsset.ReceiverTextNodeBkg.image
        let bubbleImage = stretchImage.resizableImage(withCapInsets: UIEdgeInsetsMake(30, 28, 85, 28), resizingMode: .stretch)
        self.listenVoiceButton.setBackgroundImage(bubbleImage, for: UIControlState())
        
        //设置 Highlight 背景Image
        let stretchImageHL = model.fromMe ? LYAsset.SenderTextNodeBkgHL.image : LYAsset.ReceiverTextNodeBkgHL.image
        let bubbleImageHL = stretchImageHL.resizableImage(withCapInsets: UIEdgeInsetsMake(30, 28, 85, 28), resizingMode: .stretch)
        self.listenVoiceButton.setBackgroundImage(bubbleImageHL, for: .highlighted)
        
        //设置声音 icon 的 image
        let voiceImage = model.fromMe ? LYAsset.SenderVoiceNodePlaying.image : LYAsset.ReceiverVoiceNodePlaying.image
        self.listenVoiceButton.setImage(voiceImage, for: UIControlState())
        
        //设置声音 icon 的 Edge
        self.listenVoiceButton.imageEdgeInsets = model.fromMe ? UIEdgeInsetsMake(kChatBubbleBottomTransparentHeight, 0, 0, kChatVoicePlayingMarginLeft) : UIEdgeInsetsMake(-kChatBubbleBottomTransparentHeight, kChatVoicePlayingMarginLeft, 0, 0)
        
        //设置声音 icon 的对齐方式
        self.listenVoiceButton.contentHorizontalAlignment = model.fromMe ? .right : .left
        
        if model.fromMe {
            self.listenVoiceButton.imageView!.animationImages = [
                LYAsset.SenderVoiceNodePlaying001.image,
                LYAsset.SenderVoiceNodePlaying002.image,
                LYAsset.SenderVoiceNodePlaying003.image
            ]
        }else {
            self.listenVoiceButton.imageView!.animationImages = [
                LYAsset.ReceiverVoiceNodePlaying001.image,
                LYAsset.ReceiverVoiceNodePlaying002.image,
                LYAsset.ReceiverVoiceNodePlaying003.image
            ]
        }
        
    }
    
    ///停止音频的动画
    func resetVoiceAnimation() {
        self.listenVoiceButton.imageView!.stopAnimating()
        self.listenVoiceButton.isSelected = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        guard let model = self.model else {
            return
        }
        guard let duration = model.audioModel?.duration, duration > 0 else {
            return
        }
        
        let voiceLength = 70 + 130 * CGFloat(duration / 60)
        
        //设置气泡的宽
        self.listenVoiceButton.width = min(voiceLength, kChatVoiceMaxWidth)
        //设置气泡的高度
        self.listenVoiceButton.height = kChatBubbleImageViewHeight
        //value = 头像的底部 - 气泡透明间隔值
        self.listenVoiceButton.top = self.nicknamelabel.bottom - kChatVoiceBubbleTopTransparentGapValue
        
        if model.fromMe {
            //value = 屏幕宽 - 头像的边距10 - 头像宽 - 气泡距离头像的gap值 - 文字宽 - 2倍的文字和气泡的左右距离
            self.listenVoiceButton.left = UIScreen.ts_width - kChatAvatarMarginLeft - kChatAvatarWidth - kChatBubbleMaginLeft - self.listenVoiceButton.width
            //value = 声音的左 - 秒数文字的宽 - 间隔值
            self.durationLabel.left = self.listenVoiceButton.left - self.durationLabel.width
            self.durationLabel.textAlignment = .right
        }else {
            // value = 距离屏幕左边的距离
            self.listenVoiceButton.left = kChatBubbleLeft
            // value = 声音的右+间隔值
            self.durationLabel.left = self.listenVoiceButton.right
            self.durationLabel.textAlignment = .left
        }
        
        self.durationLabel.height = self.listenVoiceButton.height
        self.durationLabel.top = self.listenVoiceButton.top
    }
    
    class func layoutHeight(_ model: ChatModel) -> CGFloat {
        if model.cellHeight != 0 {
            return model.cellHeight
        }
        
        if model.audioModel?.duration == 0 {
            return 0
        }
        
        var height: CGFloat = 0
        height += kChatAvatarMarginTop + kChatBubblePaddingBottom
        height += kChatBubbleImageViewHeight
        model.cellHeight = height
        return model.cellHeight
    }
    
}
